package elastic.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;

import java.util.List;
import java.util.Map;

public class ArchiveJob implements SimpleJob {

    /**
     *  execute方法中封装业务逻辑.
     * */
    @Override
    public void execute(ShardingContext shardingContext) {
        // 获取分片函数
        int shardingItem = shardingContext.getShardingItem();
        System.out.println("=========>>>" + shardingItem);
        String shardingParameter = shardingContext.getShardingParameter();  //0=bachelor,1=master,2=doctor


        // 从resume表中查询出未归档的一条记录。
//        String selectSql = "select * from resume where state = '未归档' limit 1";
        String selectSql = "select * from resume where state = '未归档' and education = '"+ shardingParameter +"' limit 1";
        List<Map<String, Object>> maps = JdbcUtil.executeQuery(selectSql);
        if(maps == null || maps.size() == 0){
            System.out.println(" have no data ");
            return;
        }
        // 未归档更改为已归档.
        Map<String, Object> stringObjectMap = maps.get(0);
        long id = (long) stringObjectMap.get("id");
        String name = (String) stringObjectMap.get("name");

        String updateSql = "update resume set state = '已归档' where id =?";
        JdbcUtil.executeUpdate(updateSql,id);
        // 归档这条记录，把记录插入到resume_bak
        String insertSql = "insert into resume_back select * from resume where id = ?";
        JdbcUtil.executeUpdate(insertSql,id);

    }
}
